package com.foo.DAO;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import com.foo.Entity.HistoricalData;

public class DataStoreDAOImpl implements DataStoreDAO 
{
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	Map namedParameters1 = null;
	int success =0;
	public void setDataSource(DataSource dataSource) {
	    this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void insertForum(HistoricalData[] forum,int length) 
	{
		if(length > 5)
		{
			String query = "DELETE FROM historicalstockdata";
			namedParameterJdbcTemplate.update(query, namedParameters1);

		}
		for(int i=0;i<length;i++)
		{
		String query = "INSERT INTO historicalstockdata (ticker_symbol,open_value,high_value,low_value,close_value,volume,deals_date) VALUES" +
				" (:ticker_symbol,:open_value,:high_value,:low_value,:close_value,:volume,:deals_date)";
		Map namedParameters = new HashMap();
		namedParameters.put("ticker_symbol", forum[i].getTicker_symbol());
		namedParameters.put("open_value", forum[i].getOpen_value());
		namedParameters.put("high_value", forum[i].getHigh_value());
		namedParameters.put("low_value", forum[i].getLow_value());
		namedParameters.put("close_value", forum[i].getClose_value());
		namedParameters.put("volume", forum[i].getVolume());
		namedParameters.put("deals_date", forum[i].getDeals_date());		
		success= namedParameterJdbcTemplate.update(query, namedParameters);
		System.out.println("inserted row: " +i);
		}
		
		if(1 == success)
		{
			System.out.println("Upload to db successful");
		}
	}


	@Override
	public void updateforum() {
		
		String updatequery ="UPDATE realtimestockdata SET perc_change = (((open_value-close_value)/close_value)*100) where id>1";
		Map namedParameters = new HashMap();
		int success= namedParameterJdbcTemplate.update(updatequery,namedParameters);
		if(1 == success)
		{
			System.out.println("Updated percentage change successfully");
		}
	}
	
	
 

}
